package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlDwddbgBsInfoBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlDwddbgBsInfoVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlDwddbgBsInfo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlDwddbgBsInfoMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlDwddbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料动稳定度试验检测报告Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlDwddbgBsInfoServiceImpl implements IReportLqhhlDwddbgBsInfoService {

    private final ReportLqhhlDwddbgBsInfoMapper baseMapper;
    private final TgsybgBaDetailMapper detailMapper;

    private final IBiQualificationService biQualificationService;

    private static final Long[] keys = {141L};

    /**
     * 查询沥青混合料动稳定度试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportLqhhlDwddbgBsInfoVo queryById(Long excelId){
        ReportLqhhlDwddbgBsInfoVo reportLqhhlDwddbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        reportLqhhlDwddbgBsInfoVo.setBaDetailVos(baDetailVos);
        return reportLqhhlDwddbgBsInfoVo;
    }

    /**
     * 查询沥青混合料动稳定度试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportLqhhlDwddbgBsInfoVo> queryPageList(ReportLqhhlDwddbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlDwddbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlDwddbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料动稳定度试验检测报告列表
     */
    @Override
    public List<ReportLqhhlDwddbgBsInfoVo> queryList(ReportLqhhlDwddbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqhhlDwddbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlDwddbgBsInfo> buildQueryWrapper(ReportLqhhlDwddbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlDwddbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqhhlDwddbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqhhlDwddbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqhhlDwddbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportLqhhlDwddbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportLqhhlDwddbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqhhlDwddbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqhhlDwddbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqhhlDwddbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqhhlDwddbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqhhlDwddbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqhhlDwddbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqhhlDwddbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqhhlDwddbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqhhlDwddbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqhhlDwddbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(bo.getShiyljdyq() != null, ReportLqhhlDwddbgBsInfo::getShiyljdyq, bo.getShiyljdyq());
        lqw.eq(StringUtils.isNotBlank(bo.getShijzzff()), ReportLqhhlDwddbgBsInfo::getShijzzff, bo.getShijzzff());
        lqw.eq(bo.getKongxl() != null, ReportLqhhlDwddbgBsInfo::getKongxl, bo.getKongxl());
        lqw.eq(bo.getSywd() != null, ReportLqhhlDwddbgBsInfo::getSywd, bo.getSywd());
        lqw.eq(bo.getShijmd() != null, ReportLqhhlDwddbgBsInfo::getShijmd, bo.getShijmd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportLqhhlDwddbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqhhlDwddbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportLqhhlDwddbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportLqhhlDwddbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportLqhhlDwddbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportLqhhlDwddbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportLqhhlDwddbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqhhlDwddbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqhhlDwddbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqhhlDwddbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增沥青混合料动稳定度试验检测报告
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqhhlDwddbgBsInfoBo bo) {
        ReportLqhhlDwddbgBsInfo add = BeanUtil.toBean(bo, ReportLqhhlDwddbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
            ArrayList<TgsybgBaDetail> details = new ArrayList<>(1);
            for (Long key : keys) {
                details.add(new TgsybgBaDetail(add.getExcelId(), key));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改沥青混合料动稳定度试验检测报告
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(ReportLqhhlDwddbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportLqhhlDwddbgBsInfo update = BeanUtil.toBean(bo, ReportLqhhlDwddbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), TgsybgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(ReportLqhhlDwddbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlDwddbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料动稳定度试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ReportLqhhlDwddbgBsInfoVo jcjl(ReportLqhhlDwddbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，所检技术指标均符合JTG F40-2004《公路沥青路面施工技术规范》及设计要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
